package com.lemonsama.lesson3;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class MyFrame extends Frame {
    //画画需要画笔，需要监听鼠标的位置，需要集合来存储这个点
    ArrayList points;
    //我的窗口
    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 400, 400);
        //存鼠标点击的点
        points = new ArrayList<>();
        //鼠标监听器，针对与这个窗口
        this.addMouseListener(new MyMouseListener());
        setVisible(true);
    }
    //画笔事件
    @Override
    public void paint(Graphics g) {
        //画画需要监听鼠标事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()) {
            Point next = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillRect(next.x, next.y, 10, 10);
        }
    }
    //添加一个点到界面上
    public void addPaint(Point point) {
        points.add(point);
    }
    //适配器模式
    //鼠标监听器
    private class MyMouseListener extends MouseAdapter {
        //鼠标点击
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //我们在点击的时候，就会在界面上产生一个点
            //这个点就是当前鼠标
            myFrame.addPaint(new Point(e.getX(), e.getY()));
            //每次点击都需要刷新画笔重新画点
            myFrame.repaint();
        }
    }
}
